package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_address;
import cn.com.api.service.ApiT_addressService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "t_address/")
public class ApiT_addressController {
	@Autowired
	private ApiT_addressService apiT_addressService;

	private static Logger logger = Logger.getLogger(ApiT_addressController.class);

	/**
	 * 新增
	 * 
	 * @author JW
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertT_address")
	public void insertT_address(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		String u_id = request.getParameter("u_id");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String county = request.getParameter("county");
		String address = request.getParameter("address");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String phone = request.getParameter("phone");
		String name = request.getParameter("name");
		String enable = request.getParameter("enable");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			ApiT_address t_address = new ApiT_address();
			t_address.setU_id(u_id);
			t_address.setProvince(province);
			t_address.setCity(city);
			t_address.setCounty(county);
			t_address.setAddress(address);
			t_address.setLongitude(longitude);
			t_address.setLatitude(latitude);
			t_address.setPhone(phone);
			t_address.setName(name);
			if (Util.isNull(id)) {
				if (Util.isNull(apiT_addressService.getAddressesByUserId(u_id))) {
					t_address.setEnable(1);
				} else {
					t_address.setEnable(0);
				}
				apiT_addressService.insertT_address(t_address);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				if (!Util.isNull(enable)) {
					apiT_addressService.updateEnableById(id);
					if (enable.equals("1")) {
						apiT_addressService.updateEnableZero(u_id, id);
					}
				} else {
					t_address.setId(Integer.valueOf(id));
					apiT_addressService.updateT_address(t_address);
				}
				map.put(Utils.MESSAGE, "修改成功！");
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_addressById")
	public void findT_addressById(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiT_address t_address = apiT_addressService.findT_addressById(Integer.valueOf(id));

			// return
			map.put("datas", t_address);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_addresss")
	public void findT_addresss(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiT_address> t_addresss = apiT_addressService.findT_addresss();

			// return
			map.put("datas", t_addresss);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_address")
	public void updateT_address(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String u_id = request.getParameter("u_id");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String county = request.getParameter("county");
		String address = request.getParameter("address");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String phone = request.getParameter("phone");
		String name = request.getParameter("name");
		String enable = request.getParameter("enable");

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiT_address t_address = new ApiT_address();
				// 获取原数据
				t_address = apiT_addressService.findT_addressById(Integer.valueOf(id));
				// 修改
				// t_address.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiT_addressService.updateT_address(t_address);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteT_address")
	public void deleteT_address(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiT_address address = apiT_addressService.findT_addressById(Integer.parseInt(id));
				String uid = address.getU_id();
				List<ApiT_address> addressList = apiT_addressService.getAddressesByUserId(uid);
				int count = 0;
				if (!Util.isNull(addressList)) {
					for (ApiT_address apiT_address : addressList) {
						if (!apiT_address.getId().equals(id)) {
							count++;
						}
					}
					if (count == 1) {
						apiT_addressService.updateEnableByUserId(uid);
					} else if (address.getEnable() == 1) {
						for (ApiT_address apiT_address : addressList) {
							if (!apiT_address.getId().equals(id)) {
								apiT_addressService.updateEnableById(apiT_address.getId() + "");
								break;
							}
						}
					}
				}
				Integer ret_I = apiT_addressService.deleteT_address(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
